/**
 * Copyright © 2019 dream horse Info. Tech Ltd. All rights reserved.
 *
 * @Package: com.gitee.mybatis.fl.convert
 * @author: flying-cattle
 * @date: 2019年4月9日 下午8:15:25
 */
package com.zm.mybatis.gen.util;

import com.zm.mybatis.gen.entity.ResultJson;
import com.zm.mybatis.gen.entity.ServerBasisInfo;
import freemarker.template.Configuration;
import freemarker.template.Template;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;


public class FreemarkerUtil {
    public String templatePath = "/freemarker/ftl";
    public  void setTemplatePath(String path) {
        this.templatePath = path;
    }

    public ResultJson copyGenContent(ServerBasisInfo dataModel, String templateName, String filePath) {
        ResultJson result = new ResultJson();
        StringWriter out = null;
        String fileName = dataModel.getEntityName() + messageStr(templateName, dataModel);
        try {
            // 通过FreeMarker的Confuguration读取相应的模板文件
            Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);
            // 设置模板路径
            configuration.setClassForTemplateLoading(FreemarkerUtil.class, templatePath);
            // 设置默认字体
            configuration.setDefaultEncoding("utf-8");
            // 获取模板
            Template template = configuration.getTemplate(templateName);
            //设置输出流
            out = new StringWriter();
            //模板输出静态文件
            template.process(dataModel, out);

            String contents = out.toString();
            result.setCode(1);
            result.setMessage("复制成功：" + contents);
            setSysClipboardText(contents);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    public ResultJson createFile(ServerBasisInfo dataModel, String templateName, String filePath) {
        ResultJson result = new ResultJson();
        FileWriter out = null;
        String fileName = dataModel.getEntityName() + messageStr(templateName, dataModel);
        try {
            // 通过FreeMarker的Confuguration读取相应的模板文件
            Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);
            // 设置模板路径
            configuration.setClassForTemplateLoading(FreemarkerUtil.class, templatePath);
            // 设置默认字体
            configuration.setDefaultEncoding("utf-8");
            // 获取模板
            Template template = configuration.getTemplate(templateName);
            File file = new File(filePath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            if (!file.exists()) {
                file.createNewFile();
            } else {
                file.delete();
                file.createNewFile();
                result.setCode(-1);
                result.setMessage("已经存在" + fileName + "重新生成");
            }

            //设置输出流
            out = new FileWriter(file);
            //模板输出静态文件
            template.process(dataModel, out);
            result.setCode(1);
            result.setMessage("创建" + file.getAbsolutePath() + "成功");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        result.setCode(-1);
        result.setMessage("创建" + fileName + "失败");
        return result;
    }


    //将传入的字符串内容复制到剪切板
    public static void setSysClipboardText(String writeMe) {
        Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
        Transferable tText = new StringSelection(writeMe);
        clip.setContents(tText, null);
    }



    public  String messageStr(String name, ServerBasisInfo dataModel) {
        if (name.equals("entity.ftl")) {
            name = ".java";
        } else if (name.equals("dao.ftl")) {
            name = dataModel.getDaoClassNameSuffix() + ".java";
        } else if (name.equals("mapper.ftl")) {
            name = "Mapper.xml";
        } else if (name.equals("service.ftl")) {
            name = "Service.java";
        } else if (name.equals("serviceImpl.ftl")) {
            name = "ServiceImpl.java";
        } else if (name.equals("controller.ftl")) {
            name = "Controller.java";
        } else if (name.equals("serviceTest.ftl")) {
            name = "ServiceTest.java";
        } else if (name.equals("mapperTest.ftl")) {
            name = "MapperTest.java";
        }
        return name;
    }
}
